package com.weiaibenpao.demo.chislim.sportoutdoor.presentation.module;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.umeng.socialize.PlatformConfig;
import com.weiaibenpao.demo.chislim.BuildConfig;
import com.weiaibenpao.demo.chislim.sportoutdoor.presentation.common.BaseApplication;
import com.weiaibenpao.demo.chislim.sportoutdoor.presentation.common.Constant;
import com.weiaibenpao.demo.chislim.sportoutdoor.presentation.common.utils.DrawUtil;
import com.weiaibenpao.demo.chislim.sportoutdoor.presentation.common.utils.FileUtil;
import com.weiaibenpao.demo.chislim.util.SpeechUtil;
import com.weiaibenpao.demo.chislim.util.Utils;

import org.litepal.LitePal;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

public class PedometerApplication extends BaseApplication {

    public PedometerApplication() {
        super();
    }
    public static PedometerApplication app;
    private static Map<String,Activity> destoryMap = new HashMap<>();



    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("PedometerApplication","onCreate");
        if(!Utils.getProcessName(this).equals(getPackageName())){
//            Log.e("PedometerApplication","current process is "+ Utils.getProcessName(this));
            return;//added by ZJL
        }
        app = this;
        ApplicationModule.initSingleton().onCreateMainProcess();
        LitePal.initialize(this);
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(this);
        Fresco.initialize(getApplicationContext());

        PlatformConfig.setWeixin("wx221705f86c71d1bf", "225c77b86158d2acf80eaef2c79b433f");
        //微信 appid appsecret

        PlatformConfig.setSinaWeibo("1959713611","e62e0d810039469a8852ba366afdb0bc");
        //新浪微博 appkey appsecret

        PlatformConfig.setQQZone("1105642894", "3cvNf8EoHe17VQ93");
        // QQ和Qzone appid appkey


        // 应用程序入口处调用,避免手机内存过小,杀死后台进程后通过历史intent进入Activity造成SpeechUtility对象为null
        // 注意：此接口在非主进程调用会返回null对象，如需在非主进程使用语音功能，请增加参数：SpeechConstant.FORCE_LOGIN+"=true"
        // 参数间使用“,”分隔。
        // 设置你申请的应用appid

        // 注意： appid 必须和下载的SDK保持一致，否则会出现10407错误

//        StringBuffer param = new StringBuffer();
//        param.append("appid=588020d9"  );
//        param.append(",");
//        // 设置使用v5+
//        param.append(SpeechConstant.ENGINE_MODE+"="+SpeechConstant.MODE_MSC);
//        SpeechUtility.createUtility(getApplicationContext(), param.toString());

        SpeechUtil.getInstance().init(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        DrawUtil.resetDensity(this);
        new Thread(new Runnable() {
            @Override
            public void run() {

                FileUtil.createNewFile(Constant.Path.ACCOUNT_DIR);
            }
        }).start();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }


    /**
     * 添加到销毁队列
     *
     * @param activity 要销毁的activity
     */

    public static void addDestoryActivity( String activityName,Activity activity) {
        destoryMap.put(activityName,activity);
    }
    /**
     *销毁指定Activity
     */
    public static void destoryActivity() {
        Set<String> keySet=destoryMap.keySet();

        for (String key:keySet){
            destoryMap.get(key).finish();
            Log.e("1414", "destoryActivity: " +key);
        }
    }





    /**
     * This method is for use in emulated process environments.
     * It will never be called on a production Android device,
     * where processes are removed by simply killing them;
     */

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

//    @Override
//    public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
//        super.registerActivityLifecycleCallbacks(callback);
//    }


//    @Override
//    public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
//        super.unregisterActivityLifecycleCallbacks(callback);
//    }
//
//    @Override
//    public void registerComponentCallbacks(ComponentCallbacks callback) {
//        super.registerComponentCallbacks(callback);
//    }
//
//    @Override
//    public void unregisterComponentCallbacks(ComponentCallbacks callback) {
//        super.unregisterComponentCallbacks(callback);
//    }
}
